using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Globalization;

namespace Camalon.Drawing
{
	public class LineCapStyleConverter:ExpandableInstanceConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if ( value is string ) 
			{
				string sVal = (string)value;
				sVal = sVal.Trim();
				if( sVal.Length == 0) return LineCapStyle.Default;

				LineCapStyle lineCap = LineCapStyle.Default;

				string[] args = sVal.Split(';');
				if( args.Length == 0) return lineCap;//throw base.GetConvertFromException( value);

				string sDefault = sVal.ToLower();
				if( sDefault == "none" || sDefault == "default") return lineCap;

				if( args.Length >=1 ) lineCap.Style		= (CapStyle)Enum.Parse( typeof( CapStyle ), args[0], true);
				if( args.Length >=2 ) lineCap.Size		= (CapSize)Enum.Parse( typeof( CapSize ), args[1], true);
				if( args.Length >=3 ) lineCap.Align		= (CapAlign)Enum.Parse( typeof( CapAlign ), args[2], true);
				if( args.Length >=4 ) lineCap.LineInset = float.Parse( args[3]);
				if( args.Length >=5 ) lineCap.Filled	= bool.Parse( args[4]);
				if( args.Length >=6 ) lineCap.SizeFactor = float.Parse( args[5]);//Neu

				return lineCap;

			}

			return base.ConvertFrom (context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if( value is LineCapStyle)
			{
				LineCapStyle m = (LineCapStyle) value;

				if (destinationType == typeof(string)) 
				{
					if( m.IsDefault) return "None";
					return String.Format( "{0}; {1}; {2}; {3}; {4}; {5}", m.Style, m.Size, m.Align, m.LineInset, m.Filled , m.SizeFactor);
				}

				if (destinationType == typeof(InstanceDescriptor) && value is LineCapStyle) 
				{

					LineCapStyle defaultCap = LineCapStyle.Default;

					if( m.Size == defaultCap.Size && m.Align == defaultCap.Align && m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled && m.SizeFactor == defaultCap.SizeFactor)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style});
					}
					else if( m.Align == defaultCap.Align && m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled && m.SizeFactor == defaultCap.SizeFactor)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size});
					}
					else if( m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled&& m.SizeFactor == defaultCap.SizeFactor)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align});
					}
					else if( m.Filled == defaultCap.Filled && m.SizeFactor == defaultCap.SizeFactor)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign), typeof(float) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align, m.LineInset});
					}
					else if( m.SizeFactor == defaultCap.SizeFactor)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign), typeof(float), typeof( bool) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align, m.LineInset, m.Filled});
					}
					else //ALL.. !!
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign), typeof(float), typeof( bool), typeof( float)  });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align, m.LineInset, m.Filled, m.SizeFactor});
					}


				}

			}

			return base.ConvertTo (context, culture, value, destinationType);
		}


		public object ConvertToOLD(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if( value is LineCapStyle)
			{
				LineCapStyle m = (LineCapStyle) value;

				if (destinationType == typeof(string)) 
				{
					if( m.IsDefault) return "None";
					return String.Format( "{0}; {1}; {2}; {3}; {4}", m.Style, m.Size, m.Align, m.LineInset, m.Filled );
				}

				if (destinationType == typeof(InstanceDescriptor) && value is LineCapStyle) 
				{

					LineCapStyle defaultCap = LineCapStyle.Default;

					if( m.Size == defaultCap.Size && m.Align == defaultCap.Align && m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style});
					}
					else if( m.Align == defaultCap.Align && m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size});
					}
					else if( m.LineInset == defaultCap.LineInset && m.Filled == defaultCap.Filled)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align});
					}
					else if( m.Filled == defaultCap.Filled)
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign), typeof(float) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align, m.LineInset});
					}
					else
					{
						ConstructorInfo ctor = typeof(LineCapStyle).GetConstructor(new Type[] {typeof(CapStyle), typeof(CapSize),typeof(CapAlign), typeof(float), typeof( bool) });
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Style, m.Size, m.Align, m.LineInset, m.Filled});
					}


				}

			}

			return base.ConvertTo (context, culture, value, destinationType);
		}


	}

}
